//
//  DHTitleButton.m
//  DHWeibo
//
//  Created by donghai on 15/12/9.
//  Copyright © 2015年 东海. All rights reserved.
//

#import "DHTitleButton.h"

#define kImgWidth 10 //图片宽度

@implementation DHTitleButton
#warning 只要调用init方法，内部就会调用initWithFrame方法
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //设置图片
        [self setImage:[UIImage imageNamed:@"new_dot"] forState:UIControlStateNormal];
        //设置文字颜色
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        self.backgroundColor = [UIColor purpleColor];
        
        //设置图片不被拉伸
        self.imageView.contentMode = UIViewContentModeCenter;
    }
    return self;
}

//重新布局图片和文字
-(void)layoutSubviews
{
    [super layoutSubviews];
    //重新布局titleView和imageView
    //imageView
    CGFloat imgX = self.w - kImgWidth;
    CGFloat imgY = 0;
    CGFloat imgW = kImgWidth;
    CGFloat imgH = self.h;
    self.imageView.frame = CGRectMake(imgX, imgY, imgW, imgH);
    //titleView
    CGFloat titleX = 0;
    CGFloat titleY = 0;
    CGFloat titleW = imgX;
    CGFloat titleH = self.h;
    self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
    
}
@end
